package com.wmh.baseservice.common.wechat.pay.controller;

import com.github.binarywang.wxpay.bean.request.WxPayRefundQueryRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.LogUtil;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;

@Api(tags = "微信退款")
@RestController
@RequestMapping(value = "wx/refund", produces = "text/plain;charset=utf-8")
public class WxRefundController {

    @Resource
    private WxPayService wxPayService;
    private static WxPayService staticWxPayService;

    @PostConstruct
    public void init() {
        staticWxPayService = wxPayService;
    }

    /**
     * 发起退款申请
     * @param orderNum 内部订单号
     * @param totalFee 订单金额
     * @param refundFee 退款金额
     * @param remark 退款描述，默认无
     */
    public static String refundOrder(String orderNum, BigDecimal totalFee,BigDecimal refundFee,String remark) {
        BigDecimal totalAmount = totalFee.multiply(BigDecimal.valueOf(100));
        BigDecimal refundAmount = refundFee.multiply(BigDecimal.valueOf(100));
        WxPayRefundRequest wxPayRefundRequest = WxPayRefundRequest.newBuilder().outTradeNo(orderNum)
                .outRefundNo(orderNum)
                .totalFee(totalAmount.intValue())
                .refundFee(refundAmount.intValue())
                .build();
        if (CommUtil.checkNull(remark)){
            wxPayRefundRequest.setRefundDesc(remark);
        }
        try {
            WxPayRefundResult result = staticWxPayService.refund(wxPayRefundRequest);
            if ("SUCCESS".equals(result.getReturnCode()) && "SUCCESS".equals(result.getResultCode())) {
                return "ok";
            } else {
                return result.getReturnMsg();
            }
        } catch (WxPayException e) {
            LogUtil.error("微信退款异常：{}",e);
            throw new ServiceException();
        }
    }

    /**
     * 查询退款状态
     * @param orderNum 内部订单号
     */
    public static String queryRefund(String orderNum){
        WxPayRefundQueryRequest wxPayRefundQueryRequest = WxPayRefundQueryRequest.newBuilder().outTradeNo(orderNum).build();
        try {
            WxPayRefundQueryResult result = staticWxPayService.refundQuery(wxPayRefundQueryRequest);
            if ("SUCCESS".equals(result.getReturnCode()) && "SUCCESS".equals(result.getResultCode())) {
                return result.getRefundRecords().get(0).getRefundStatus();
            } else {
                return result.getReturnMsg();
            }
        } catch (WxPayException e) {
            LogUtil.error("微信查询退款状态异常：{}",e);
            throw new ServiceException(e.toString());
        }
    }

}
